package com.threeonemeeting.model;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.telephony.TelephonyManager;

/**
 * 
 * @author zhangzhongqing
 * @desc 用户的移动网络相关的信息
 */
public class NetInfo {

	private Context context;

	private String strNetworkType; // 网络类型,WiFi，蓝牙，GSM等等
	private String strNetworkDetail; // 关于移动网络的更细的网络
	private String strMobileName; // 移动运营商类型

	private static NetInfo networkInfo = new NetInfo();

	public static NetInfo getInstance(Context ctx) {
		networkInfo.context = ctx;
		networkInfo.initValue();
		return networkInfo;
	}

	private NetInfo() {
	}

	private void initValue() {
		TelephonyManager mTelephonyMgr = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);

		// 获取当前的运营商
		strMobileName = mTelephonyMgr.getNetworkOperatorName();

		// 设置网络类型
		initNetwork();
	}

	private void initNetwork() {

		TelephonyManager mTelephonyMgr = (TelephonyManager) context
				.getSystemService(Context.TELEPHONY_SERVICE);

		ConnectivityManager connectivityManager = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);// 获取系统的连接服务
		NetworkInfo activeNetInfo = connectivityManager.getActiveNetworkInfo();// 获取网络的连接情况
		if (activeNetInfo == null) {
			strNetworkType = "";
			strNetworkDetail = "";
			return;
		}
		if (activeNetInfo.getType() == ConnectivityManager.TYPE_WIFI) {

			strNetworkType = "WiFi";
			strNetworkDetail = "WiFi";

		} else if (activeNetInfo.getType() == ConnectivityManager.TYPE_MOBILE) {

			/**
			 * 获取网络类型
			 * 
			 * NETWORK_TYPE_CDMA 网络类型为CDMA NETWORK_TYPE_EDGE 网络类型为EDGE
			 * NETWORK_TYPE_EVDO_0 网络类型为EVDO0 NETWORK_TYPE_EVDO_A 网络类型为EVDOA
			 * NETWORK_TYPE_GPRS 网络类型为GPRS NETWORK_TYPE_HSDPA 网络类型为HSDPA
			 * NETWORK_TYPE_HSPA 网络类型为HSPA NETWORK_TYPE_HSUPA 网络类型为HSUPA
			 * NETWORK_TYPE_UMTS 网络类型为UMTS
			 * 
			 * 在中国，联通的3G为UMTS或HSDPA，移动和联通的2G为GPRS或EGDE，电信的2G为CDMA，电信的3G为EVDO
			 */

			int nType = mTelephonyMgr.getNetworkType();
			if (nType == TelephonyManager.NETWORK_TYPE_CDMA) {
				strNetworkType = "CDMA";
				strNetworkDetail = "CDMA";
			} else if (nType == TelephonyManager.NETWORK_TYPE_1xRTT) {
				strNetworkType = "CDMA2000";
				strNetworkDetail = "1xRTT";
			} else if (nType == TelephonyManager.NETWORK_TYPE_EDGE) {
				strNetworkType = "GPRS";
				strNetworkDetail = "EDGE";
			} else if (nType == TelephonyManager.NETWORK_TYPE_EVDO_0) {
				strNetworkType = "CDMA2000";
				strNetworkDetail = "EVDO0";
			} else if (nType == TelephonyManager.NETWORK_TYPE_EVDO_A) {
				strNetworkType = "CDMA2000";
				strNetworkDetail = "EVDOA";
			} else if (nType == TelephonyManager.NETWORK_TYPE_GPRS) {
				strNetworkType = "GRPS";
				strNetworkDetail = "GRPS";
			} else if (nType == TelephonyManager.NETWORK_TYPE_UMTS) {
				strNetworkType = "WCDMA";
				strNetworkDetail = "UMTS";
			} else {
				strNetworkType = "Unknow";
				strNetworkDetail = "Unknow";
			}
		}
	}

	public String getOperatorName() {
		return this.strMobileName;
	}

	public String getNetworkType() {
		return this.strNetworkType;
	}

	public String getNetworkName() {
		return this.strNetworkDetail;
	}
}